package com.lin.controller;

import com.lin.entity.Account;
import com.lin.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileInputStream;

@RestController
@RequestMapping("/accounts")
public class AccountController {
    private AccountService accountService;

    @Autowired
    public void setAccountService(AccountService accountService) {
        this.accountService = accountService;
    }

    @GetMapping("/show")
    public String show() {
        System.out.println("show");
        int i = 1 / 0;
        return "like everything!";
    }

    @GetMapping(value = "/show2", produces = "application/json")
    public Account show2() throws Exception {
        System.out.println("show2");
        // 模拟文件不存在异常
        FileInputStream fileInputStream = new FileInputStream("xxx/xxx.xxx");
        fileInputStream.close();
        return new Account(1, "lin", 1000);
    }

    @PutMapping("/transfer")
    public String update() {
        System.out.println("transfer");
        String a = "Tom";
        String b = "Jerry";
        if (accountService.transfer(a, b, 100)) return "transfer successful...";
        return "transfer failed...";
    }
}
